//
//  ImageDetailView.m
//  Product_NEWS
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 FCH. All rights reserved.
//
#define SPACE 135 / 4 * SCREEN_WIDTH
#import "ImageDetailView.h"

@interface ImageDetailView ()



@end



@implementation ImageDetailView
- (void)dealloc {
    [_model release];
    [_layerViewNote release];
    [_layerView release];
    [_title release];
    [_imagesScrollView release];
    [_note release];
    [_page release];
    [_btnBack release];
    [_btnComment release];
    [_btnShare release];
    [_btnSave release];
    [_toolBar release];
    [super dealloc];
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.imagesScrollView];
        [self addSubview:self.layerView];
        [self addSubview:self.layerViewNote];
        [self addSubview:self.toolBar];
    }
    return self;
}

- (UILabel *)title {
    if (!_title) {
        self.title = [[[UILabel alloc] initWithFrame:CGRectMake(10 * SCREEN_WIDTH, 20 *SCREEN_HEIGHT, 355* SCREEN_WIDTH, 30*SCREEN_HEIGHT)] autorelease];
        _title.textColor = [UIColor whiteColor];
        _title.textAlignment = NSTextAlignmentCenter;
    }
    return [[_title retain] autorelease];
}

- (UIScrollView *)imagesScrollView {
    if (!_imagesScrollView) {
        self.imagesScrollView = [[[UIScrollView alloc] initWithFrame:self.bounds] autorelease];
        _imagesScrollView.showsHorizontalScrollIndicator = NO;
        _imagesScrollView.showsVerticalScrollIndicator = NO;
        _imagesScrollView.pagingEnabled = YES;
        _imagesScrollView.bounces = NO;
        _imagesScrollView.directionalLockEnabled = YES;
    }
    return [[_imagesScrollView retain] autorelease];
}
// http://blog.it985.com/7986.html
//标题渐变层
- (UIView *)layerView {
    if (!_layerView) {
        self.layerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375 * SCREEN_WIDTH, 100*SCREEN_HEIGHT)] autorelease];
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.bounds = _layerView.bounds;
        gradientLayer.borderWidth = 0;
        gradientLayer.frame = _layerView.bounds;
        gradientLayer.colors =  @[(id)[[UIColor blackColor] CGColor], (id)[[UIColor clearColor] CGColor]];
//                gradientLayer.startPoint = CGPointMake(1.0, 1.0);
                gradientLayer.endPoint = CGPointMake(0.5, 0.8);
        [_layerView.layer insertSublayer:gradientLayer atIndex:0];
        [_layerView addSubview:self.title];
        [self addSubview:_layerView];
    }
    return [[_layerView retain] autorelease];
}

//简介渐变层
- (UIView *)layerViewNote {
    if (!_layerViewNote) {
        self.layerViewNote = [[[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 170*SCREEN_HEIGHT, 375 * SCREEN_WIDTH, 170*SCREEN_HEIGHT)] autorelease];
        CAGradientLayer *gradientLayer = [CAGradientLayer layer];
        gradientLayer.bounds = _layerViewNote.bounds;
        gradientLayer.borderWidth = 0;
        gradientLayer.frame = _layerViewNote.bounds;
        gradientLayer.colors =  @[(id)[[UIColor clearColor] CGColor], (id)[[UIColor blackColor] CGColor]];
        //        gradientLayer.startPoint = CGPointMake(0.1, 0.1);
        gradientLayer.endPoint = CGPointMake(0.5* SCREEN_WIDTH, 0.5*SCREEN_HEIGHT);
        [_layerViewNote.layer insertSublayer:gradientLayer atIndex:0];
        [_layerViewNote addSubview:self.note];
        [self addSubview:_layerViewNote];
    }
    return [[_layerViewNote retain] autorelease];
}
- (UITextView *)note {
    if (!_note) {
        self.note = [[[UITextView alloc] initWithFrame:CGRectMake(10* SCREEN_WIDTH, 0, 355 * SCREEN_WIDTH , 140*SCREEN_HEIGHT)] autorelease];
        //设置textView是否可以拖动
        _note.scrollEnabled = YES;
        _note.editable = NO;
        //设置textView自适应高度
        _note.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _note.font = [UIFont systemFontOfSize:15 * SCREEN_WIDTH];
        _note.textColor = [UIColor whiteColor];
        _note.backgroundColor = [UIColor clearColor];
        _note.textAlignment = NSTextAlignmentLeft;
    }
    return [[_note retain] autorelease];
}

- (UIButton *)btnBack {
    if (!_btnBack) {
        self.btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnBack.frame = CGRectMake(28* SCREEN_WIDTH, 0, 30* SCREEN_WIDTH, 25*SCREEN_HEIGHT);
        _btnBack.contentEdgeInsets = UIEdgeInsetsMake(5* SCREEN_WIDTH, 5*SCREEN_HEIGHT, 5* SCREEN_WIDTH, 10*SCREEN_HEIGHT);
        
        [_btnBack setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"XHNBack" ofType:@"png"]] forState:UIControlStateNormal];
    }
    return [[_btnBack retain] autorelease];
}
- (UIButton *)btnShare {
    if (!_btnShare) {
        self.btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnShare.frame = CGRectMake(SPACE * 2* SCREEN_WIDTH + 40* SCREEN_WIDTH, 1*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT);
        
        [_btnShare setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"XHNPicShare" ofType:@"png"]]forState:UIControlStateNormal];
    }
    return [[_btnShare retain] autorelease];
}

- (UIButton *)btnSave {
    if (!_btnSave) {
        self.btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnSave.frame = CGRectMake(SPACE * 3* SCREEN_WIDTH + 80* SCREEN_WIDTH, 0, 23* SCREEN_WIDTH, 23*SCREEN_HEIGHT);
        
        [_btnSave setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DazStarOutline" ofType:@"png"]] forState:UIControlStateNormal];
    }
    return [[_btnSave retain] autorelease];
}
- (UIButton *)btnComment {
    if (!_btnComment) {
        self.btnComment = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnComment.frame = CGRectMake(SPACE * 4* SCREEN_WIDTH + 120* SCREEN_WIDTH, 3*SCREEN_HEIGHT, 20* SCREEN_WIDTH, 20*SCREEN_HEIGHT);
        [_btnComment setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"XHNPicComment" ofType:@"png"]] forState:UIControlStateNormal];
    }
    return [[_btnComment retain] autorelease];
}
- (UILabel *)lblPage {
    if (!_page) {
        self.page = [[[UILabel alloc] initWithFrame:CGRectMake(315* SCREEN_WIDTH, 0, 60* SCREEN_WIDTH, 20*SCREEN_HEIGHT)] autorelease];
        _page.text = @"...";
        _page.textColor = [UIColor whiteColor];
    }
    return [[_page retain] autorelease];
}

- (UIView *)toolBar {
    if (!_toolBar) {
        self.toolBar = [[[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 40*SCREEN_HEIGHT, 375 * SCREEN_WIDTH, 40*SCREEN_HEIGHT)] autorelease];
        _toolBar.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.9];
        [self.toolBar  addSubview:self.btnBack];
        [self.toolBar  addSubview:self.btnSave];
        [self.toolBar  addSubview:self.btnShare];
        [self.toolBar  addSubview:self.btnComment];
        [self.toolBar  addSubview:self.lblPage];
    }
    return [[_toolBar retain] autorelease];
}

#pragma mark - setter
-(void)setModel:(ImagesDetailModel *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
        self.title.text = model.setname;
        self.note.text = [NSString stringWithFormat:@"      %@", model.photos[0][@"note"]];
        
        self.page.text = [NSString stringWithFormat:@"1/%@",model.imgsum];
    }
}










@end
